package com.library.controller;

import com.library.common.base.ApiResult;
import com.library.common.utils.CommonUtil;
import com.library.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import sun.security.util.SecurityConstants;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 公共接口
 *
 * @author myg
 * @date 2023-07-26  16:06:24
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/common")
@Api(tags = "公共接口", value = "公共接口接口")
public class CommonController {

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private SmsService smsService;

	@ApiOperation(value = "注册发送手机验证码")
	@GetMapping("/code/sms")
	@ResponseBody
	public ApiResult sendSmsCode(@RequestParam("mobile") String mobile,
						 HttpServletRequest request) throws IOException {
		String code = CommonUtil.getCode();
		redisTemplate.opsForValue().set(
				"sms-code-".concat(mobile), code,
				60, TimeUnit.SECONDS);
//		redissonClient.getBucket("sms-code-".concat(mobile)).set(code, 900, TimeUnit.SECONDS);
		boolean flag = smsService.sendSms(mobile, code);
		return ApiResult.ok(flag);
	}


}
